const tool = require('../tools/tool')
const patrol = require('../methods/patrol')
const permissions = require('../methods/permissions')
const system = require('../methods/system')
const user = require('../methods/user')
const alert = require('../methods/alert')
const formatDateToMySQL = require('../tools/formatDateToMySQL')

module.exports = {
    store: {
        exp: '保存巡查信息',
        method: 'post',
        auth: true,
        async func(data) {

            let userData = await tool.methods.user().getSingleUserInfoByUserId(data.currentUserId)
            if (tool.formatDateString(new Date(userData.lastPatrolCreatedAt)) != tool.formatDateString(new Date())) {
                await tool.methods.user().updateInfo(data.currentUserId, {
                    lastPatrolCreatedAt: formatDateToMySQL(new Date()),
                    patrolCount: 1
                })
            } else {
                await tool.methods.user().updateInfo(data.currentUserId, {
                    lastPatrolCreatedAt: formatDateToMySQL(new Date()),
                    patrolCount: userData.patrolCount + 1
                })
            }
            userData = await tool.methods.user().getSingleUserInfoByUserId(data.currentUserId)
            if (userData.patrolCount > (await tool.methods.settings().getItem('dailyPatrolLimitPerUser'))) throw '你每日可新建巡查数量已到上限'

            let patrolData = data.param.patrolData
            let currentTime = system.getServerTime()
            if (new Date(patrolData.endTime) < new Date(currentTime)) {
                throw '负责人处理截止时间不能早于当前时间'
            }
            return await patrol.store(patrolData)
        }
    },
    setImage: {
        exp: '设置图片',
        method: 'post',
        auth: true,
        async func(data) {
            let fileName = data.param.fileName
            let id = data.param.id
            await patrol.setImage(id, fileName)
        }
    },
    publish: {
        exp: '发布',
        method: 'post',
        auth: true,
        async func(data) {
            if (data.param.images) {
                for (let i = 0; i < data.param.images.length; i++) {
                    await patrol.setImage(data.param.id, data.param.images[i])
                }
            }
            await patrol.publish(data.param.id)
            // await tool.sendSubscribeMessage(data.currentUserOpenId, 'xxxxxxxxxxxxxxxxxxxx', 'xxxxxxxxxxxxxxxxxxxx', new Date(), 'xxxxxxxxxxxxxxxxxxxx')
            alert.patrol.add(data.param.id)
        }
    },
    preview: {
        exp: '首页预览',
        method: 'get',
        async func(data) {
            let re = await patrol.get.list(
                data.query.finalId,
                data.query.size ? parseInt(data.query.size) : undefined,
                data.currentUserId,
                data.currentUserId === undefined ? 4 : undefined,
                data.query.username,
                data.query.phone,
                data.query.departmentId ? parseInt(data.query.departmentId) : undefined,
                data.query.createdAt_start,
                data.query.createdAt_end,
                data.query.endTime_start,
                data.query.endTime_end
            )
            // console.log(re)
            return re
        }
    },
    singleDetail: {
        exp: '单个巡查详细信息',
        method: 'get',
        async func(data) {
            if (!data.query.id) throw '未提供巡查ID'
            let re = await patrol.get.single(data.query.id ? parseInt(data.query.id) : undefined, data.currentUserId)
            return re
        }
    },
    isExist: {
        exp: '检查巡查是否存在',
        method: 'post',
        async func(data) {
            if (await patrol.exist(data.param.patrolId)) {

            } else {
                throw '巡查不存在'
            }
        }
    },
    delete: {
        exp: '删除巡查',
        method: 'post',
        auth: true,
        async func(data) {
            if (!data.param.id) throw '未提供巡查id'
            data.param.id = parseInt(data.param.id)
            let allow = false
            let currentUserData = await user.getSingleUserInfoByUserId(data.currentUserId)
            if (currentUserData.isAdmin) {
                allow = true
            }
            let patrolData = await patrol.getHead(data.param.id)
            if (patrolData.userId == currentUserData.id && patrolData.status == 1) allow = true
            if (!allow) throw '权限不足或者非删除状态'
            await patrol.delete(data.param.id)

            await tool.methods.user().updateInfo(patrolData.userId, {
                patrolCount: currentUserData.patrolCount - 1
            })
        }
    },
    delay: {
        exp: '推迟巡查',
        method: 'post',
        auth: {
            per1: true,
        },
        async func(data) {
            await patrol.delay(data.param.patrolId, data.currentUserId, data.param.datetime)
            await alert.patrol.delay(data.param.patrolId)
        }
    },
    singleSimple: {
        exp: '获取单个列表内展示的巡查数据',
        method: 'get',
        auth: true,
        async func(data) {
            if (!data.query.id) throw '巡查不存在'
            const patrol = require('../methods/patrol')
            return await patrol.get.single(parseInt(data.query.id), data.currentUserId, true)
        }
    },
    handle: {
        exp: '处理巡查',
        method: 'post',
        auth: {
            per1: true,
        },
        async func(data) {
            await patrol.handle(data.param.patrolId, data.currentUserId, data.param.form)
            await alert.patrol.handle(data.param.patrolId)
        }
    },
    deleteFile: {
        exp: '删除文件',
        method: 'post',
        auth: true,
        async func(data) {
            await tool.deleteFile(data.param.fileName)
        }
    },
    review: {
        exp: '驳回巡查',
        method: 'post',
        auth: {
            per: 2
        },
        async func(data) {
            await patrol.review(data.param.patrolId, data.currentUserId, data.param.desc)
            await alert.patrol.review(data.param.patrolId)
        }
    },
    priority: {
        exp: '催办巡查',
        method: 'post',
        auth: true,
        async func(data) {
            await patrol.priority(data.param.patrolId, data.currentUserId)
            await alert.patrol.priority(data.param.patrolId, data.currentUserId)
        }
    },
    feedBack: {
        exp: '评价巡查',
        method: 'post',
        auth: true,
        async func(data) {
            await patrol.feedBack(data.param.patrolId, data.currentUserId, data.param.rating, data.param.desc)
        }
    },
    myTasks: {
        exp: '获取我的事务巡查',
        method: 'post',
        auth: true,
        async func(data) {
            const d = data.param
            return await patrol.get.userTasks(
                d.groupName,
                d.campusId,
                d.categoryId,
                d.isDelay,
                d.userId,
                d.finalId,
                d.size
            )
        }
    },
    search: {
        exp: '搜索巡查',
        method: 'post',
        auth: true,
        async func(data) {
            return await patrol.search(data.param)
        }
    },
    searchWithTotal: {
        exp: '搜索巡查计算总数',
        method: 'post',
        auth: true,
        async func(data) {
            return await patrol.search(data.param, true)
        }
    },
    deleteAllNotHandle: {
        exp: '删除全部未处理巡查',
        method: 'post',
        auth: {
            admin: true
        },
        async func(data) {
            await patrol.deleteAllNotHandle()
        }
    },
    genExcel: {
        exp: '生成表格文件',
        method: 'post',
        auth: {
            admin: true
        },
        async func(data) {
            patrol.exportToExcel(data.param, data.currentUserId)
        }
    },
    edit: {
        campusCategory: {
            exp: '巡查修改校区类别',
            method: 'post',
            auth: true,
            async func(data) {
                await tool.methods.patrol().edit.campusCatrgory(
                    data.param.patrolId,
                    data.currentUserId,
                    data.param.campusId,
                    data.param.categoryId
                )
                await alert.patrol.edit.campusCategory(data.param.patrolId)
            }
        }
    }
}